//
//  SearchResultViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/1.
//  Copyright © 2016年 KX. All rights reserved.

#import "CellOfSearchResultSmall.h"
#import "SearchResultViewController.h"
#import "FreeTravelMacro.h"
#import "FreeTravelNetwork.h"
#import "CellOfPostSearchResult.h"
#import "CellOfUserSearchResult.h"
#import "CellOfDiscountSearchResult.h"
#import "CellOfDestinationSearchResult.h"
@interface SearchResultViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,UITextFieldDelegate>
@property (nonatomic, strong) UITextField *searchTextField;

@property (nonatomic, strong) UICollectionView *smallCollectionView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, assign)NSInteger labelIndex;// 记录是第几个label
@property (nonatomic, strong)NSIndexPath *oldIndexPath;//记录第几个label的indexPath

@end

@implementation SearchResultViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self.collectionView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.oldIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    
    [self createTop];
    [self createCollectionView];
    [self createLineView];
    [self createKVOAction];
}

- (void)createTop{
    self.searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(60, 20, SCREEN_WIDTH-120, 44)];
    self.searchTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.searchTextField.delegate = self;
    [self.view addSubview:self.searchTextField];
    UIButton *search = [UIButton buttonWithType:UIButtonTypeCustom];
    search.frame = CGRectMake(SCREEN_WIDTH-50, 20, 40, 44);
    [self.view addSubview:search];
    [search setTitle:@"取消" forState:UIControlStateNormal];
    [search setTitleColor:COLORFORFREETRAVEL forState:UIControlStateNormal];
    [search addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    
    
}
//点击textfield
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [self goBack];  
    return YES;
}
- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-110);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 110, SCREEN_WIDTH, SCREEN_HEIGHT-110) collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor orangeColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.pagingEnabled = YES;

    [self.collectionView registerClass:[CellOfPostSearchResult class] forCellWithReuseIdentifier:@"CellOfPostSearchResult"];
    [self.collectionView registerClass:[CellOfUserSearchResult class] forCellWithReuseIdentifier:@"CellOfUserSearchResult"];
    [self.collectionView registerClass:[CellOfDiscountSearchResult class] forCellWithReuseIdentifier:@"CellOfDiscountSearchResult"];
    [self.collectionView registerClass:[CellOfDestinationSearchResult class] forCellWithReuseIdentifier:@"CellOfDestinationSearchResult"];
    
    UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc]init];
    layout1.itemSize = CGSizeMake(SCREEN_WIDTH/4, 44);
    layout1.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout1.minimumLineSpacing = 0;
    layout1.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.smallCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 44) collectionViewLayout:layout1];
    [self.view addSubview:self.smallCollectionView];
    
    
    self.smallCollectionView.backgroundColor = [UIColor whiteColor];
    self.smallCollectionView.delegate = self;
    self.smallCollectionView.dataSource = self;
    self.smallCollectionView.showsHorizontalScrollIndicator = NO;
    [self.smallCollectionView registerClass:[CellOfSearchResultSmall class] forCellWithReuseIdentifier:@"SearchResultViewControllerSmall"];
}
- (void)createLineView{
    self.lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 108, SCREEN_WIDTH/4, 2)];
    [self.view addSubview:self.lineView];
    self.lineView.backgroundColor = COLORFORFREETRAVEL;
}
- (void)createKVOAction{
    [self.collectionView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"nil"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    CGFloat x = [[change objectForKey:@"new"] CGPointValue].x;
    CGFloat lineX = x/4;

    self.lineView.transform = CGAffineTransformMakeTranslation(lineX, 0);
}
// 滑动减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == self.collectionView) {
        // 当前大collectionview 第几个item
        NSInteger currentItemCount = scrollView.contentOffset.x/SCREEN_WIDTH;
        // 根据item获取indexpath
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:currentItemCount inSection:0];
        // 根据indexPath 获取当前小collectionviewCell
        // 进行强转
        CellOfSearchResultSmall *currentCell = (CellOfSearchResultSmall *)[self.smallCollectionView cellForItemAtIndexPath:indexPath];
        // 将字体变色
        currentCell.label.textColor = COLORFORFREETRAVEL;
        // 恢复字体颜色
        // 根据之前,再取出cell协议的方法中记录的item下标
        NSIndexPath *indexPathPast = [NSIndexPath indexPathForItem:_labelIndex inSection:0];
        // 加判断防止划一点点就松开, 并没有去新的界面,则文字颜色不变
        if (indexPath != indexPathPast) {
            // 根据indexPath 获取当前的小collectionviewCell
            CellOfSearchResultSmall *pastCell = (CellOfSearchResultSmall *)[self.smallCollectionView cellForItemAtIndexPath:indexPathPast];
            pastCell.label.textColor = [UIColor blackColor];
        }
        // 记录是第几个label
        self.labelIndex = currentItemCount;
        // 记录label的indexpath
        self.oldIndexPath = indexPath;
        
        
        if (self.searchTextField.text.length == 0) {
            if (indexPath.item ==1) {
                CellOfPostSearchResult *cell = (CellOfPostSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.search;
                
            }else if (indexPath.item ==2){
                CellOfDiscountSearchResult *cell = (CellOfDiscountSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.search;
            }else if (indexPath.item == 3){
                CellOfUserSearchResult *cell = (CellOfUserSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.search;
                
            }else{
                CellOfDestinationSearchResult *cell = (CellOfDestinationSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.search;
                
            }
        }else{
            if (indexPath.item ==1) {
                CellOfPostSearchResult *cell = (CellOfPostSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.searchTextField.text;
            }else if (indexPath.item ==2){
                CellOfDiscountSearchResult *cell = (CellOfDiscountSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.searchTextField.text;
            }else if (indexPath.item == 3){
                CellOfUserSearchResult *cell = (CellOfUserSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.searchTextField.text;
            }else{
                CellOfDestinationSearchResult *cell = (CellOfDestinationSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.searchTextField.text;
            }
        }
    }
}
// 点击小cell触发
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.smallCollectionView) {
        [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        CellOfSearchResultSmall *cell = (CellOfSearchResultSmall *)[self.smallCollectionView cellForItemAtIndexPath:indexPath];
        cell.label.textColor = COLORFORFREETRAVEL;
        CellOfSearchResultSmall *pastCell = (CellOfSearchResultSmall *)[self.smallCollectionView cellForItemAtIndexPath:self.oldIndexPath];
        if (indexPath != self.oldIndexPath) {
            pastCell.label.textColor = [UIColor blackColor];
        }
        self.labelIndex = self.oldIndexPath.item;
        self.oldIndexPath = indexPath;
        if (self.searchTextField.text.length == 0) {
            if (indexPath.item ==1) {
                CellOfPostSearchResult *cell = (CellOfPostSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.search;
            }else if (indexPath.item ==2){
                CellOfDiscountSearchResult *cell = (CellOfDiscountSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.search;
            }else if (indexPath.item == 3){
                CellOfUserSearchResult *cell = (CellOfUserSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.search;

            }else{
                CellOfDestinationSearchResult *cell = (CellOfDestinationSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.search;
            }
        }else{
            if (indexPath.item ==1) {
                CellOfPostSearchResult *cell = (CellOfPostSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.searchTextField.text;
            }else if (indexPath.item ==2){
                CellOfDiscountSearchResult *cell = (CellOfDiscountSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.searchTextField.text;
            }else if (indexPath.item == 3){
                CellOfUserSearchResult *cell = (CellOfUserSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.searchTextField.text;

            }else{
                CellOfDestinationSearchResult *cell = (CellOfDestinationSearchResult *)[self.collectionView cellForItemAtIndexPath:indexPath];
                cell.mosaicString = self.searchTextField.text;
            }
        }
        
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    if (collectionView == self.collectionView) {
        if (indexPath.item == 0) {
                 
            CellOfDestinationSearchResult *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfDestinationSearchResult" forIndexPath:indexPath];
            if (self.searchTextField.text.length == 0) {
                cell.mosaicString = self.search;
            }else{
                cell.mosaicString = self.searchTextField.text;
                
            }

            
            return cell;
        }else if (indexPath.item ==1){
            CellOfPostSearchResult *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfPostSearchResult" forIndexPath:indexPath];
            if (self.searchTextField.text.length == 0) {
                cell.mosaicString = self.search;
            }else{
                cell.mosaicString = self.searchTextField.text;
            }
            
            return cell;
        }else if(indexPath.item == 2){
            CellOfDiscountSearchResult *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfDiscountSearchResult" forIndexPath:indexPath];
            if (self.searchTextField.text.length == 0) {
                cell.mosaicString = self.search;
            }else{
                cell.mosaicString = self.searchTextField.text;
            }
            return cell;
        }else{
            CellOfUserSearchResult *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfUserSearchResult" forIndexPath:indexPath];
            if (self.searchTextField.text.length == 0) {
                cell.mosaicString = self.search;
            }else{
                cell.mosaicString = self.searchTextField.text;
            }
            return cell;
        }
    }else{
        CellOfSearchResultSmall *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchResultViewControllerSmall" forIndexPath:indexPath];
        
        NSArray *array = @[@"目的地",@"帖子",@"折扣",@"用户"];
        cell.label.text = array[indexPath.item];
        cell.label.textAlignment = 1;
        //保证开启第一个字是绿色
        if (indexPath.row == self.labelIndex) {
            cell.label.textColor = COLORFORFREETRAVEL;
        }else{
            cell.label.textColor = [UIColor blackColor];
        }
        if (indexPath.row == self.oldIndexPath.row) {
            cell.label.textColor = COLORFORFREETRAVEL;
        }else{
            cell.label.textColor = [UIColor blackColor];
        }
        return cell;
    }
}
- (void)goBack{
    [self.collectionView removeObserver:self forKeyPath:@"contentOffset"];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
